﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WR.BL.BE;

namespace WR.UI.MobWebApp
{
    public partial class detcot : System.Web.UI.Page
    {
        private UserBE _objUsr;
        private SalesOrderBE _lvSO;

        private void checkLogin()
        {
            lblErrorMsg.InnerHtml = string.Empty;
            _objUsr = (UserBE)Session["UserBE"];
            if (_objUsr == null)
            {
                Response.Redirect("login.aspx");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                this.checkLogin();
                if (!Page.IsPostBack)
                {
                    _lvSO = (SalesOrderBE)Session["SalesOrderBE"];
                    if (_lvSO == null || _lvSO.Detalle == null || _lvSO.Detalle.Count == 0)
                    {
                        throw new Exception();
                    }

                    string strPos = Request.QueryString["pos"];
                    DetailBE objDet = null;
                    if (!string.IsNullOrEmpty(strPos))
                    {
                        Session["strPos"] = strPos;
                        objDet = _lvSO.Detalle.Find(X => X.Pos == strPos);
                        if (objDet != null)
                        {
                            txtCantidad.Value = string.Format("{0:0}", objDet.Cant);

                            lblCodigo.InnerHtml = objDet.Material.Codigo;
                            lblDescripcion.InnerHtml = objDet.Material.Descripcion;
                            lblUMedida.InnerHtml = objDet.UMedida;
                            lblPrecio.InnerHtml = string.Format("{0:0,0.00}", objDet.Material.Precio);
                            lblTotal.InnerHtml = string.Format("{0:0,0.00}", objDet.Total);
                            lblPromocion.InnerHtml = string.Format("{0:0,0.00}", objDet.DescuentoTotal); //RGF 20111027
                        }
                    }
                    else { throw new Exception(); }
                }
            }
            catch
            {
                Response.Redirect("crearcot.aspx");
            }
        }

        protected void lnkOk_Click(object o, EventArgs e)
        {
            lblErrorMsg.InnerHtml = string.Empty;
            try
            {
                _lvSO = (SalesOrderBE)Session["SalesOrderBE"];
                if (_lvSO == null || _lvSO.Detalle == null || _lvSO.Detalle.Count == 0)
                {
                    Response.Redirect("crearcot.aspx");
                }
                string strPos = Session["strPos"].ToString();
                _lvSO.Detalle.Find(X => X.Pos == strPos).Cant = Convert.ToDecimal(txtCantidad.Value.Trim());
                //DetailBE objDet = null;
                //objDet = _lvSO.Detalle.Find(X => X.Pos == strPos);
                //objDet.Cant = Convert.ToDecimal(txtCantidad.Value.Trim());
                //objSO.Detalle.Add(objDet);
                Session["SalesOrderBE"] = _lvSO;
                Response.Redirect("crearcot.aspx");
                //lblOkMsg.InnerHtml = "Se añadió el material " + objDet.CodMat;
            }
            catch (Exception ex)
            {
                lblErrorMsg.InnerHtml = ex.Message;
            }
        }
    }
}
